import { combineReducers, AnyAction } from "redux"
import * as actionTypes from './constants'
import { filterList } from '@/api/utils'

const initalState = {
    MainImg:{
        tab:[],
        img:[]
    },
    MainCities:{
        cities:[]
    },
    Local:{
        local:[]
    },
    
    search:{
        // swipers:[],
        searchresult:[],
        suggestList:[],
        hotList:[],
        searchHistory:[],
        resultList:[]
    },
    good:{
        goodStuffTabs: [],
        goodStuffList: [],
        hasMore: true,
        isShowImage: false,
        imageShowList: [],
        imageDefaultIndex: 0,
        loading: true,
    }
}

const MainImgReducer=(state=initalState.MainImg,action:AnyAction)=>{
    switch(action.type){
        case actionTypes.SET_MAIN_TAB:
            return {
                ...state,
                tab:action.data
            }
            break;
        case actionTypes.SET_MAIN_IMG:
            return {
                ...state,
                img:action.data
            }
            break;
        default:
            return state 
    }
}

const MainCitiesReducer=(state=initalState.MainCities,action:AnyAction)=>{
    // console.log(1)
    switch(action.type){ 
        case actionTypes.SET_CITIES:
            return {
                ...state,
                cities:action.data
            }
            break;
        default:
            return state 
    }
}

const MainLocalReducer=(state=initalState.Local,action:AnyAction)=>{
    // const {type, data} = action;
    // switch (type) {
    //     case actionTypes.SET_LOCAL:
    //         if (data[0] == 7) return {...state, local: data};
    //         let newData = filterList(data)
    //         return {...state, local: [...newData]}
    //     // case actionTypes.CHANGE_LOADING:
    //     //     return {...state, loading: data}
    //     default:
    //         return state
    // }

    // console.log(1)
    switch(action.type){ 
        case actionTypes.SET_LOCAL:
            return {
                ...state,
                local:action.data
            }
            break;
        default:
            return state 
    }
}

// search
const searchReducer = (state = initalState.search,action:AnyAction) => {
    switch(action.type) {
        case actionTypes.SET_HOT_KEYWOEDS:
            return {
                ...state,
                hotList: action.data
            }
        case actionTypes.GET_SEARCHRESULT:
            // console.log(state,action.data)
            let result = state.hotList.filter(
                todo=>todo.key.includes(action.data)
                // todo => todo.desc.includes(action.data) || todo.classify.includes(action.data)
                )
            return {
                    ...state,
                    searchresult: result
                } 
        case actionTypes.SET_SEARCH_SWIPER:
            return {
                ...state,
                swipers:action.data
            }
        case actionTypes.SET_SEARCH_HOTKEY:
            return {
                ...state,
                hotkey:action.data
            }
        case actionTypes.SET_SEARCH_SUGGEST:
            let results = state.hotkey.filter(
                todo=>todo.key.includes(action.data)
                // todo => todo.desc.includes(action.data) || todo.classify.includes(action.data)
                ) 
            return {
                // ...state,
                // suggestList:result
            }
        case actionTypes.SET_SEARCH_HISTORY:
            return {
                ...state,
                searchHistory:action.data
            }
        case actionTypes.ADD_SEARCH_HISTORY:
            return {
                ...state,
                searchHistory:addHist(Object.assign([],state.searchHistory),action.data)
            }
        case actionTypes.CLEAR_SEARCH_HISTORY:
            // 清空Localstorage
            setSearchHistory_loc([]);
            return {
                ...state,
                searchHistory:[]
            }
        case actionTypes.SET_SEARCH_RESULT:
            return {
                ...state,
                resultList:action.data
            }
        default:
            return state;
    }
}



// qunaer
const likeReducer=(state=initalState.good, action: AnyAction) => {
    const {type, data} = action;
    switch (type) {
        case actionTypes.CHANGE_GOOD_STUFF_TABS:
            return {
                ...state,
                 goodStuffTabs: data
                }
        case actionTypes.CHANGE_GOOD_STUFF_LIST:
            return {
                ...state, 
                goodStuffList: data
            }
        case actionTypes.ADD_GOOD_STUFF_LIST:
            let newList = [...state.goodStuffList, ...data]
            // console.log(newList,'newlist',state.goodStuffList)
            return {
                ...state, 
                goodStuffList: [...newList]
            }
        case actionTypes.CHANGE_HAS_MORE:
            return {
                ...state, 
                hasMore: data
            }
        case actionTypes.CHANGE_IS_SHOW_IMAGE:
            return {
                ...state, 
                isShowImage: data
            }
        case actionTypes.CHANGE_IMAGE_SHOW_LIST:
            return {
                ...state, 
                imageShowList: data
            }
        case actionTypes.CHANGE_IMAGE_DEFAULT_INDEX:
            return {
                ...state, 
                imageDefaultIndex: data
            }
        case actionTypes.CHANGE_LOADING:
            return {
                ...state, 
                loading: data
            }
        default:
            return state
    }
}



export default combineReducers({
    MainImg:MainImgReducer,
    MainCities:MainCitiesReducer,
    Local:MainLocalReducer,
    search:searchReducer,
    goodStuff:likeReducer
})